package com.gugee.tiktok.data.web.service.impl;

import com.gugee.tiktok.data.common.msg.BloggerMessage;
import com.gugee.tiktok.data.web.config.WebConfig;
import com.gugee.tiktok.data.web.service.BloggersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

/**
 * 播主服务实现
 */
@Slf4j
@Service("bloggersService")
public class BloggersServiceImpl implements BloggersService {

    @Autowired
    WebConfig webConfig;
    /**
     * kafka
     */
    @Autowired
    KafkaTemplate<Long, BloggerMessage> kafkaTemplateBlogger;
    /**
     * 向kafka队列中插入更新播主请求
     * @param bloggerId
     * @param region
     * @param bloggerType
     */
    @Override
    public boolean sendBloggerMsg(Long bloggerId,String region,String bloggerType) {
        BloggerMessage bloggerMessage = new BloggerMessage();
        bloggerMessage.setUid(bloggerId);
        bloggerMessage.setRegion(region);
        bloggerMessage.setBloggerType(bloggerType);
        try{
            kafkaTemplateBlogger.send(webConfig.getBloggerOnlineTopic(),bloggerMessage);
        }catch (Exception e){
            log.info(String.format("向kafka队列中发送更新播主请求失败：bloggerId: %s , region: %s ,bloggerType: %s .",
                    bloggerId,region,bloggerType),e);
            return false;
        }
        return true;
    }
}
